package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.*;
import com.ymttest.utils.YMTDateUtil;

public class LiveRoomWapper {
	SqlManager manage;

	public LiveRoomWapper() {
		manage = new SqlManager("ymtlivetv");
	}

	/**
	 * 根据liveid查询liveromm信息
	 * 
	 * @param id
	 * @return
	 */
	public LiveRoom selectLiveRoomById(Integer id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		List<LiveRoom> liveroom = (List<LiveRoom>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLiveRoomById", params);
		manage.commit();
		return liveroom.get(0);
	}

	public Map selectLastLiveOnlineStatById(Integer liveid) {
		List<Map> liveroom = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLastLiveOnlineStatById", liveid);
		manage.commit();
		if (liveroom == null || liveroom.isEmpty()) {
			return null;
		}
		return liveroom.get(0);
	}

	/**
	 * 查询可以开始播放的liveroom 状态已结束的被剔除
	 * 
	 * @param userid
	 * @return
	 */
	public List<LiveRoom> selectRunningLiveRoomByUserid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		List<LiveRoom> liveroom = (List<LiveRoom>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectRunningLiveRoomByUserid", params);
		manage.commit();
		return liveroom;
	}

	/**
	 * 最后一个加入的liveroom
	 * 
	 * @return
	 */
	public LiveRoom selectLastLiveRoom() {
		List<LiveRoom> liveroom = (List<LiveRoom>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLastLiveRoom", null);
		manage.commit();
		return liveroom.get(0);
	}

	/**
	 * 查询商品信息
	 * 
	 * @param liveid
	 * @return
	 */
	public List<Map> selectLiveproductByLiveid(Integer liveid) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		List<Map> liveroom = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLiveproductByLiveid", m);
		manage.commit();
		return liveroom;
	}

	/**
	 * 查询置顶商品
	 * 
	 * @param liveid
	 * @return
	 */
	public List<Map> selectTopLiveproductByLiveid(Integer liveid) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		List<Map> liveroom = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectTopLiveproductByLiveid", m);
		manage.commit();
		return liveroom;
	}

	/**
	 * 查询直播间在线的人
	 * 
	 * @param liveid
	 * @param uid
	 * @return
	 */
	public List<Map> selectLivemember(Integer liveid, Integer uid) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("uid", uid);
		List<Map> liveroom = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLivemember", m);
		manage.commit();
		return liveroom;
	}

	/**
	 * 根据直播号商品id查询商品
	 * 
	 * @param liveid
	 * @param productid
	 * @return [{ProductNo=0, Status=1, IsTop=false, Action=0, UserId=2254514,
	 *         CreateTime=2017-10-25 15:44:12.0, LiveId=299,
	 *         UpdateTime=2017-10-25 15:44:12.0, LiveProductId=350,
	 *         ProductId=da541da7-eacf-4fd1-bf86-ff30e120427c, SortNo=999000}]
	 */
	public List<Map> selectLiveproductByIdAndPid(Integer liveid, String productid) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("productid", productid);
		List<Map> liveroom = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLiveproductByIdAndPid", m);
		manage.commit();
		return liveroom;
	}

	/**
	 * 根据liveid查询在线人数比心数换算记录
	 * 
	 * @param id
	 * @return
	 */
	public Map selectLiveRoomStatById(Integer id) {
		Map m = new HashMap<>();
		m.put("id", id);
		List<Map> liveroomstatus = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLiveRoomStatById", m);
		manage.commit();
		if (liveroomstatus != null && liveroomstatus.size() > 0) {
			return liveroomstatus.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 删除观众观看记录
	 * 
	 * @param liveid
	 * @param uid
	 * @return
	 */
	public int deleteLivemember(Integer liveid, Integer uid) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("uid", uid);
		int i = manage.delete("com.ymttest.database.mapping.LiveRoomMapper.deleteLivemember", m);
		manage.commit();
		return i;
	}

	/**
	 * 通过productid更新商品信息
	 * 
	 * @param productid
	 *            商品id
	 * @param istop
	 *            是否置顶
	 * @param status
	 *            状态 且 status是1（在售）0是不可售
	 * @param action
	 *            是0（未删除） -1是删除
	 * @return
	 */
	public int updateLiveProductStatusById(Integer LiveProductId, Integer istop, Integer status, Integer action) {
		Map m = new HashMap<>();
		m.put("LiveProductId", LiveProductId);
		m.put("istop", istop);
		m.put("status", status);
		m.put("action", action);
		int i = manage.update("com.ymttest.database.mapping.LiveRoomMapper.updateLiveProductStatusById", m);
		manage.commit();
		return i;
	}

	/**
	 * 更新直播开始结束时间
	 * 
	 * @param starttime
	 *            格式 2017-10-24 16:50:13
	 * @param endtime
	 *            格式 2017-10-24 16:50:13
	 * @param id
	 *            liveid
	 * @return
	 */
	public int updateLiveRoomTime(String starttime, String endtime, Integer id) {
		Map m = new HashMap<>();
		m.put("id", id);
		m.put("starttime", starttime);
		m.put("endtime", endtime);
		int i = manage.update("com.ymttest.database.mapping.LiveRoomMapper.updateLiveRoomTime", m);
		if (i == 1) {
			manage.commit();
		} else {
			manage.rollback();
		}
		return i;
	}

	/**
	 * 更新推流状态？
	 * 
	 * @param liveid
	 * @param livestatus
	 * @return
	 */
	public int updateLiveRoomPushStatus(Integer liveid, Integer livestatus) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("livestatus", livestatus);
		int i = manage.update("com.ymttest.database.mapping.LiveRoomMapper.updateLiveRoomPushStatus", m);
		manage.commit();
		return i;
	}

	/**
	 * 查询直播数量
	 * 
	 * @return
	 */
	public Long selectCountLiveroom(Integer uid) {
		Map param = new HashMap<>();
		param.put("uid", uid);
		Map m = (Map) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectCountLiveroom", param).get(0);
		manage.commit();
		return (Long) m.get("count");
	}

	/**
	 * 直播订阅
	 * 
	 * @return
	 */
	public List<Map> selectLiveSubscribeById(Integer liveid, Integer userid) throws InterruptedException {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("userid", userid);
		List<Map> LiveSubscribeMap = manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLiveSubscribeById", m);
		manage.commit();
		return LiveSubscribeMap;
	}

	public List<Map> selectLivevisitorById(String deviceid) throws InterruptedException {
		Map m = new HashMap<>();
		m.put("deviceid", deviceid);
		List<Map> LiveSubscribeMap = manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLivevisitorById", m);
		manage.commit();
		return LiveSubscribeMap;
	}

	public Long selectCountLiveroomByStatus(Integer status) throws InterruptedException {
		Map param = new HashMap<>();
		param.put("status", status);
		Long count = (Long) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectCountLiveroomByStatus", param).get(0);
		manage.commit();
		return count;
	}

	public int updateLiveRoomAction(Integer liveid, Integer action) {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		m.put("action", action);
		int i = manage.update("com.ymttest.database.mapping.LiveRoomMapper.updateLiveRoomAction", m);
		manage.commit();
		return i;
	}

	public Map selectMessagetaskByMsgtime(String msgtime) throws InterruptedException {
		Map m = new HashMap<>();
		m.put("msgtime", msgtime);
		List<Map> messagetaske = manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectMessagetaskByMsgtime", m);
		manage.commit();
		if(messagetaske!=null&&!messagetaske.isEmpty()){
			return messagetaske.get(0);
		}else{
			return null;
		}
	}
	public Integer selectLivedebugCountById(String id) throws InterruptedException {
		Map m = new HashMap<>();
		m.put("id", id);
		Integer count = (Integer) manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLivedebugCountById", m).get(0);
		manage.commit();
		return count;
	}
	public Map selectLivereplayById(String liveid) throws InterruptedException {
		Map m = new HashMap<>();
		m.put("liveid", liveid);
		List<Map> messagetaske = manage.getSelectResults("com.ymttest.database.mapping.LiveRoomMapper.selectLivereplayById", m);
		manage.commit();
		if(messagetaske!=null&&!messagetaske.isEmpty()){
			return messagetaske.get(0);
		}else{
			return null;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(FormatUtil.beanToGSONString(new LiveRoomWapper().selectLastLiveOnlineStatById(3524)));
	}

}
